#ifndef __BSP_TOUCH_H_
#define __BSP_TOUCH_H_
#include "gd32f4xx.h"
#include "bsp_soft_iic.h"
#include "stdbool.h"
/*************************************************
* TP_CS -> PB12 //gt911 复位引脚
* TP_MOSI -> PB15 //iic SDA信号
* TP_MISO -> PB14
* TP_SCK -> PB13 //iic sck信号
* TP_IRQ -> PD11 //触摸屏数据有效中断信号
**************************************************/
#pragma pack(1)
typedef struct{
	uint8_t track_id;
	union{
		struct{
			uint8_t low;
			uint8_t high;
		}byte;
		uint16_t value;
	}x;
	union{
		struct{
			uint8_t low;
			uint8_t high;
		}byte;
		uint16_t value;
	}y;
	union{
		struct{
			uint8_t low;
			uint8_t high;
		}byte;
		uint16_t value;
	}size;
	uint8_t res;
}gt911_point_t;

typedef struct{
	uint8_t product_id[4];
	uint8_t firmware_version[2];
	union{
		struct{
			uint8_t low;
			uint8_t high;
		}byte;
		uint16_t value;
	}x_coordinate_resolution;
	union{
		struct{
			uint8_t low;
			uint8_t high;
		}byte;
		uint16_t value;
	}y_coordinate_resolution;
	uint8_t vendor_id;
	uint8_t res0[3];
	union{
		struct{
			uint8_t number_point : 4;
			uint8_t reserved : 2;
			uint8_t large_detect : 1;
			uint8_t buffer_status : 1;
		}bf;
		uint8_t value;
	}touch_status;
	gt911_point_t point1;
	gt911_point_t point2;
	gt911_point_t point3;
	gt911_point_t point4;
	gt911_point_t point5;
}gt911_touch_data_t;
#pragma pack()

typedef struct{
	uint16_t touch_status;
	uint16_t point1;
	uint16_t point2;
	uint16_t point3;
	uint16_t point4;
	uint16_t point5;
}gt911_reg_addr_t;


void bsp_touch_init(void);
uint8_t gt911_read_byte(soft_iic_dev_t *dev, uint16_t readAddr);
void gt911_read_buf(soft_iic_dev_t *dev, uint16_t readAddr, uint8_t *buf, uint16_t len);
void gt911_write_byte(soft_iic_dev_t *dev, uint16_t writeAddr, uint8_t ch);
void gt911_write_buf(soft_iic_dev_t *dev, uint16_t writeAddr, uint8_t *buf, uint16_t len);
void bsp_touch_scan(void);
void bsp_touch_status_clean(void);

bool bsp_touch_ok(void);
void bsp_touch_get_xy(uint32_t *x, uint32_t *y);
#endif
